package com.robot.agent.controller;

import com.alibaba.fastjson.JSON;
import com.robot.agent.client.MemberClient;
import com.robot.agent.client.MerchantClient;
import com.robot.agent.client.OrdersClient;
import com.robot.agent.pojo.Agent;
import com.robot.agent.service.AgentService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersClient ordersClient;

    @Autowired
    private HttpServletRequest request;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * agent根据agentId查找今天订单
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("findTodayOrders/{page}/{size}")
    public Result findOrdersByAgentIdAndToday(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_agent");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对代理商是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindSubOrdersByAgentIdAndToday(claims.getId(), page, size)));
    }

    /**
     * agent根据agentId查找昨天订单
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("findYesterdayOrders/{page}/{size}")
    public Result findOrdersByAgentIdAndYesterday(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_agent");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对代理商是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindSubOrdersByAgentIdAndYesterday(claims.getId(), page, size)));
    }

    /**
     * orders根据agentId查找最近7日订单
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("findWeekOrders/{page}/{size}")
    public Result findOrdersByAgentIdAndWeek(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_agent");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对代理商是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindSubOrdersByAgentIdAndWeek(claims.getId(), page, size)));
    }

    /**
     * orders根据agentId查找总订单
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("findTotalOrders/{page}/{size}")
    public Result findOrdersByAgentIdAndTotal(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_agent");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("claims.getId()=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对代理商是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindSubOrdersByAgentIdAndTotal(claims.getId(), page, size)));
    }
}
